/**
 * 
 */
package com.healthshare.ui.news;

import com.healthshare.R;
import com.healthshare.activity.news.ActicleListActivity;
import com.healthshare.busi.news.NewsClassBusi;
import com.healthshare.busi.news.NewsListBusi;
import com.healthshare.busi.share.ShareBusi;
import com.healthshare.busi.share.ShareListBusi;
import com.healthshare.parse.news.NewsClassParse;
import com.healthshare.parse.news.NewsClassParse.NewSClass;
import com.healthshare.parse.news.NewsListParse;
import com.healthshare.parse.news.NewsListParse.NewsLisItem;
import com.palmedia.framework.business.BaseBusi;
import com.palmedia.framework.myinterface.UiCallBack;
import com.palmedia.framework.ui.CustomListView;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.TypedValue;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
/**
 * @author xudeliang
 *
 */
public class NewsView  extends LinearLayout implements UiCallBack 
{

	private Context context;
	private LinearLayout buttonGroup;
	private HashMap<String,ArrayList<NewsLisItem>> newsListItems = new HashMap<String,ArrayList<NewsLisItem>>();
	private CustomListView customListView;
	private ProgressBar progressBar;
	
	private NewsListBusi  nlb;
	
	
	private int currentTabIndex=0;
	public NewsView(Context context,AttributeSet as)
	{
		super(context);
		this.context=context;
		LayoutInflater.from(context).inflate(R.layout.news, this, true);
		
		buttonGroup= (LinearLayout) findViewById(R.id.buttonboard);
		
		HorizontalScrollView hsv= (HorizontalScrollView) findViewById(R.id.hsv);
		hsv.setHorizontalScrollBarEnabled(false);
		
		customListView =(CustomListView) findViewById(R.id.list);
		
		progressBar = (ProgressBar) findViewById(R.id.wait_progressBar);
		initStaticUi();
		reqNewsClass();
		
//		ShareBusi sb = new ShareBusi(this);
//		sb.content="xxx";
//		
//		sb.iExecute();
//		
//		ShareListBusi slb = new ShareListBusi(this);
//		//slb.content="xxx";
//		
//		slb.iExecute();
	}

	/**
	 * @author xudeliang
	 * 请求资讯分类数据
	 */
	private void reqNewsClass()
	{
		NewsClassBusi  ncb = new NewsClassBusi(this);
		ncb.iExecute();
		progressBar.setVisibility(View.VISIBLE);
		//ncb=null;
	}
	
	/**
	 * @author xudeliang
	 * 更新新闻大类ui
	 */
	private void updateNewsClassUI(NewsClassParse ncp)
	{
		for(int i=0;i<ncp.newSClasss.size();i++)
		{
			Button news=new Button(context);
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
			lp.gravity=Gravity.CENTER;
			buttonGroup.addView( news, lp);
			news.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);//.setTextSize(size)
			news.setText(ncp.newSClasss.get(i).title);
			news.setTag(ncp.newSClasss.get(i));
			ncp.newSClasss.get(i).index = i;
			if(i==0)
			{
				reqNewsClassItem(ncp.newSClasss.get(i).type);
			}
			news.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v)
				{
					// TODO Auto-generated method stub
						
					NewSClass nc = (NewSClass)v.getTag();
					if(currentTabIndex==nc.index)
					{
						return;
					}
					
					currentTabIndex = nc.index;
					ArrayList<NewsLisItem> tmpNLI  = newsListItems.get(nc.type);
					if(null!=tmpNLI && tmpNLI.size()>0)
					{
						//有数据则初始化
						updateNewsClassItemUI(tmpNLI);
					}
					else
					{
						if(null!=nlb)
						{
							nlb.iCancle();
							nlb=null;
						}
						reqNewsClassItem(nc.type);
					}
				}

			});
		}
		
	}
	
	/**
	 * @author xudeliang
	 * 请求资讯分类相数据
	 * 
	 * type 表示请求的类型
	 */
	private void reqNewsClassItem(String type)
	{
		nlb = new NewsListBusi(this);
		nlb.type=type;
		nlb.iExecute();
		progressBar.setVisibility(View.VISIBLE);
		
		customListView.removeAllView();
	}
	
	/**
	 * @author xudeliang
	 * 更新新闻小类ui
	 */
	private void updateNewsClassItemUI(ArrayList<NewsLisItem> items)
	{
		customListView.removeAllView();
		for(int i=0;i<items.size();i++)
		{
        	TextView text = new TextView(context);
        	text.setBackgroundColor(Color.rgb(214, 215, 222));
        	text.setBackgroundResource(R.drawable.newslistshap);
        	text.setText(items.get(i).title);
        	text.setTag(items.get(i));
        	text.setGravity(Gravity.CENTER);
        	text.setTextColor(Color.BLACK);
        	text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
        	
        	text.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v)
				{
					// TODO Auto-generated method stub
					
					Bundle bundle=new Bundle();
					Intent intent=new Intent();
					intent.putExtra("bundle",bundle);
					NewsLisItem nli = (NewsLisItem)v.getTag();
					bundle.putString("listid",nli.id);
				   	intent.setClass(context,ActicleListActivity.class);
				   	context.startActivity(intent);
				}
        		
        	});
			customListView.addViewToLast(text);
			customListView.onRefreshComplete();
		}
		
	}
	
	private void initStaticUi()
	{

	}


	/* (non-Javadoc)
	 * @see com.palmedia.framework.myinterface.UiCallBack#uiCallBack(com.palmedia.framework.business.BaseBusi)
	 */
	@Override
	public void uiCallBack(BaseBusi obj)
	{
		// TODO Auto-generated method stub
		progressBar.setVisibility(View.GONE);
		if(obj instanceof NewsClassBusi)
		{
			NewsClassParse ncp = (NewsClassParse)obj.getBaseStruct();
			if(null ==ncp ||ncp.newSClasss.size()<=0)
			{
				Toast.makeText(context, "数据加载失败，请重试。", Toast.LENGTH_SHORT).show();
				return;
			}
			updateNewsClassUI(ncp);
			
		}
		else if(obj instanceof NewsListBusi)
		{
			NewsListParse ncp = (NewsListParse)obj.getBaseStruct();
			
			if(null ==ncp ||ncp.newsLisItems.size()<=0)
			{
				Toast.makeText(context, "数据加载失败，请重试。", Toast.LENGTH_SHORT).show();
				return;
			}
			newsListItems.put(ncp.type, ncp.newsLisItems);
			updateNewsClassItemUI(ncp.newsLisItems);
		}
		
		
	}
	
}
